// pages/upload/upload.js
const app = getApp();
const db = wx.cloud.database();

Page({
  // 页面的初始数据
  data: {
    kinds: [],
    detail_kind: '水果',
    detail_goods: 0,
    detail_mode: '自提',
    address: '',
    class_index: 0,
    goods_index: 0,
    mode_index: 0,
    iamges_list: [],
    img_url: '',
    now_or_future: ["期货", "现货"],
    trans_mode: ["自提", "邮递"],
    title: '',
    price: 0.00,
    num: 0,
    phone: '',
    now_date: ""
  },


  // 发布
  jump: function () {
    var that = this
    // 判断手机号是否正确
    var phone_len = this.data.phone.length
    if (phone_len != 11) {
      wx.showToast({
        title: '手机号长度错误',
        icon: 'error'
      })
      return;
    }
    wx.showToast({
      title: '上传中',
      icon: 'loading',
    })
    wx.cloud.uploadFile({
      cloudPath: "images_folder/" + Math.random().toString(),
      filePath: that.data.iamges_list[0], // 文件路径
      success: function (res) {
        // get resource ID
        that.setData({
          img_url: res.fileID,
        })
        if (that.data.img_url) {
          that.upDatabase();
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '请上传图片',
          icon: 'error',
        })

      },
    })
  },

  upDatabase: function () {
    var that = this;
    db.collection('goods').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        address: that.data.address,
        title: that.data.title.concat('  '),
        detail: that.data.detail,
        price: that.data.price,
        phone: that.data.phone,
        num: that.data.num,
        detail_mode: that.data.detail_mode,
        detail_kind: that.data.detail_kind,
        detail_goods: that.data.detail_goods,
        img_url: that.data.img_url,
        now_date: that.data.now_date,
        upload_time: new Date(),
      },
      success: function (res) {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        var get_id = res._id
        wx.showToast({
          title: '成功发布',
          icon: 'success'
        })
        wx.navigateTo({
          url: '/pages/goods_detail/goods_detail?id=' + get_id
        })
        return;
      }
    })
  },

  // 点我选择分类
  chooseClass: function (e) {
    this.setData({
      class_index: e.detail.value,
      detail_kind: this.data.kinds[e.detail.value],
    })
  },
  chooseGoods: function (e) {
    this.setData({
      goods_index: e.detail.value,
      detail_goods: e.detail.value,
    })
  },
  chooseMode: function (e) {
    this.setData({
      mode_index: e.detail.value,
      detail_mode: this.data.trans_mode[e.detail.value],
    })
  },
  chooseDate: function (e) {
    this.setData({
      now_date: e.detail.value,
    })
  },
  // 选择图片，获得临时地址
  chooseImages: function (e) {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          iamges_list: res.tempFilePaths
        });
      },
    });
  },

  // 填写信息上传
  input_title: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  input_price: function (e) {
    this.setData({
      price: e.detail.value
    })
  },
  input_address: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  input_nums: function (e) {
    this.setData({
      num: e.detail.value
    })
  },
  input_phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  input_detail: function (e) {
    this.setData({
      detail: e.detail.value
    })
  },

  // 生命周期函数--监听页面加载

  onLoad: function (options) {
    var date = new Date()
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();

    this.setData({
      now_date: year + '-' + month + '-' + strDate
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this

    db.collection('classify').doc('classify_001').get({
      success: function (res) {
        // res.data 包含该记录的数据
        that.setData({
          kinds: res.data.kinds,
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})